package com.dubbo.provider.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.dubbo.provider.bean.Org;
import com.dubbo.provider.bean.OrgAccount;
import com.dubbo.provider.dao.OrgAccountDao;
import com.dubbo.provider.dao.OrgDao;
import com.dubbo.remote.OrgFacade;
import com.dubbo.remote.common.BusinessException;
import com.dubbo.remote.common.PageInfo;
import com.dubbo.remote.request.OrgCreateReq;
import com.dubbo.remote.request.OrgModifyReq;
import com.dubbo.remote.request.OrgQueryReq;
import com.dubbo.remote.response.OrgCreateResp;
import com.dubbo.remote.response.OrgItem;
import com.dubbo.remote.response.OrgQueryResp;

@Transactional(rollbackFor = Exception.class)
public class OrgProvider implements OrgFacade {
	@Autowired
	private OrgDao orgDao;
	@Autowired
	private OrgAccountDao orgAccountDao;

	/**
	 * 分页查询
	 * 
	 * @param kinCode 机构类型
	 * @param page    分页参数（page为自定义的对象）
	 * @return
	 */
	@Override
	public OrgQueryResp list(OrgQueryReq req) {
		Assert.notNull(req.getKinCode(), "机构类型不能为空");
		if ("ORG".equals(req.getKinCode())) {
			throw new BusinessException("您没有权限查询机构数据");
		}
		OrgQueryResp resp = new OrgQueryResp();
		List<Org> poList = null;
		PageInfo pageInfo = req.getPageInfo();
		if (pageInfo == null) {
			// 不分页
			poList = orgDao.queryList(req.getKinCode());
		} else {
			// 分页
			poList = orgDao.queryList(req.getKinCode(), pageInfo.getBegin(), pageInfo.getPageSize());
			int totalCount = orgDao.count(req.getKinCode());
			pageInfo.setCount(totalCount);
			resp.setPageInfo(pageInfo);
		}
		List<OrgItem> items = new ArrayList<OrgItem>();
		if (poList != null) {
			for (Org org : poList) {
				OrgItem item = new OrgItem();
				BeanUtils.copyProperties(org, item);
				items.add(item);
			}
		}
		resp.setData(items.toArray(new OrgItem[items.size()]));
		return resp;
	}

	@Override
	public OrgCreateResp createOrg(OrgCreateReq req) {
		OrgCreateResp orgCreateResp = orgDao.createOrg(req);
		if (orgCreateResp.getId() != null) {
			String orgCode = orgCreateResp.getOrgCode();
			// 创建成功后，会创建账户
			OrgAccount orgAccount = new OrgAccount();
			orgAccount.setOrgCode(orgCode);
			orgAccount.setCategory(req.getCategory());
			orgAccount.setAccountName(req.getAccountName());
			orgAccount.setAccountNo(req.getAccountNo());
			orgAccount.setBankName(req.getBankName());
			orgAccount.setBankCardMobile(req.getBankCardMobile());
			Boolean createAccountResult = orgAccountDao.createOrgAccount(orgAccount);
			Assert.isTrue(createAccountResult, "操作失败");
		}
		return orgCreateResp;
	}

	@Override
	public Boolean modifyOrg(OrgModifyReq req) {
		return orgDao.modifyOrg(req);
	}

	@Override
	public Boolean enableOrg(Long id) {
		return orgDao.enableOrg(id);
	}

	@Override
	public Boolean disableOrg(Long id) {
		return orgDao.disableOrg(id);
	}

}
